package com.apes.scm.pur.redemption.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.contract.model.Contract;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Redemption.CONST_TABLE_NAME)
public class Redemption extends Model {
    public static final String CONST_TABLE_NAME = "pur_redemption";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "入库冲红", prefix = "CH%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    @Comment("订单号")
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("部门")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "clerk_id", referencedColumnName = "id", updatable = false)
    @Comment("业务员")
    private PartyRole clerk;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("仓库库区")
    private Location location;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id", nullable = false)
    @Comment("供应商")
    private PartyRole supplier;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_id", referencedColumnName = "id")
    @Comment("合同号")
    private Contract contract;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cooperation_id", referencedColumnName = "id")
    @Comment("合作方式")
    private Cooperation cooperation;

    @Column(name = "source", length = 32)
    @Comment("上游单据号码")
    private String source;

    @Column(name = "source_type", length = 64)
    @Comment("上游单据类型")
    private String sourceType;

    @Column(name = "supplier_ref", length = 30)
    @Comment("第三方单据")
    private String supplierRef;

    @Column(name = "origin", length = 30)
    @Comment("原始单号")
    private String origin;

    @Column(name = "origin_business_type", length = 32)
    @Comment("原始单据类型")
    private String originBusinessType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "store_reason", referencedColumnName = "id")
    @Comment("原因")
    private Enumeration storeReason;

    @Column (name ="note")
    @Comment("备注")
    private String note;

    @Column(name = "amt_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("税金")
    private double amtTax = 0;

    @Column(name = "amt_untaxed", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("不含税金额")
    private double amtUntaxed = 0;

    @Column(name = "amt_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总计")
    private double amtTotal = 0;

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "订单状态", values = {
            @State(id = "sent", name = "新建"),
            @State(id = "approve", name = "审核"),
            @State(id = "delete", name = "删除"),
            @State(id = "done", name = "已完成")})
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    @Comment("创建人")
    private PartyRole createUid;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @OneToMany(mappedBy = "redemption", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RedemptionItem> redemptionItems = new HashSet();

    public Set<RedemptionItem> getRedemptionItems() {
        for (RedemptionItem redemptionItem:redemptionItems) {
            redemptionItem.setRedemption(this);
        }
        return redemptionItems;
    }
}






















